package com.atguigu.edu.realtime.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PhoenixUtil {
    public static void executesql(String sql) {

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DruidDSUtil.getConnection();
            ps = conn.prepareStatement(sql);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //从phoenix表中查询数据
    //public static 返回值类型 方法名（参数列表）
    //<T>声明List <T>使用
    public static <T> List<T> queryList(String sql, Class<T> clz) {
        List<T> resList = new ArrayList<>();
        //参考配置信息的预加载
        //获取连接
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;


        try {
            connection = DruidDSUtil.getConnection();
            //创建数据库操作对象
            preparedStatement = connection.prepareStatement(sql);
            //执行SQL语句
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            //处理结果集
            while (resultSet.next()) {
                //创建一个对象，用于封装每一条查询结果
                T Obj = clz.newInstance();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnName(i);
                    Object columnValue = resultSet.getObject(i);
                    BeanUtils.setProperty(Obj, columnName, columnValue);
                }
                resList.add(Obj);
            }
        } catch (Exception e) {
            throw new RuntimeException("从phoenix表中查询数据发生了异常");
        } finally {
            //释放资源
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return resList;
    }
//[{"ID":"1","TM_NAME":"Redm"}, {"ID":"10","TM_NAME":"欧莱雅"}, {"ID":"11","TM_NAME":"香奈儿"}, {"ID":"2","TM_NAME":"苹果"},
// {"ID":"3","TM_NAME":"华为"}, {"ID":"4","TM_NAME":"TCL"}, {"ID":"5","TM_NAME":"小米"}, {"ID":"6","TM_NAME":"长粒香"},
// {"ID":"7","TM_NAME":"金沙河"}, {"ID":"8","TM_NAME":"索芙特"}, {"ID":"9","TM_NAME":"CAREMiLLE"}]
    //每条查询结果封装成一个json对象，放到集合中去。

    public static void main(String[] args) {


        System.out.println(queryList("select * from EDU_REALTIME.DIM_BASE_TRADEMARK", JSONObject.class));
    }
}
